package roger;

import java.util.List;

public class ToXml {
	
	public String toXml(String nom, List<Garage> garages) {
		StringBuilder sb = new StringBuilder();
		sb.append("<societe>\n");
		sb.append("\t<nom>");
		sb.append(nom);
		sb.append("</nom>\n");
		if (garages.size() > 0) {
			sb.append("\t<garages>\n");
			for (Garage g : garages) {
				sb.append(visiteGarage(g, "\t\t") + "\n");
			}
			sb.append("\t</garages>\n");
		}
		sb.append("</societe>\n");
		return sb.toString();
	}
	
	public String visiteGarage(Garage g, String indent) {
		StringBuilder sb = new StringBuilder();
		sb.append(indent + "<garage>\n");
		sb.append(indent + "\t<nom>" + g.getNom() + "</nom>\n");
		sb.append(indent + "\t<adresse>" + g.getAdresse() + "</adresse>\n");
		sb.append(indent + "\t<capacite>" + g.getCapacite() + "</capacite>\n");
		sb.append(indent + "\t<nbRepEnCours>" + g.getNbRepEnCours() + "</nbRepEnCours>\n");
		if (g.getNbRepEnCours() > 0) {
			sb.append(indent + "\t<reparations>\n");
			for (int i = 0; i < g.getNbRepEnCours(); i++) {
				sb.append(visiteReparation(g.getReparations()[i], indent + "\t\t") + "\n");
			}
			sb.append(indent + "\t</reparations>\n");
		}
		sb.append(indent + "</garage>");
		return sb.toString();
	}
	
	public String visiteReparation(Reparation r, String indent) {
		StringBuilder sb = new StringBuilder();
		if (r.getClass().equals(Voiture.class)) {
			sb.append(visiteVoiture((Voiture)r, indent));
		}
		else if (r.getClass().equals(Moto.class)) {
			sb.append(visiteMoto((Moto)r, indent));
		}
		return sb.toString();
	}
	
	public String visiteVoiture(Voiture v, String indent) {
		StringBuilder sb = new StringBuilder();
		sb.append(indent + "<voiture>\n");
		sb.append(indent + "\t<date>" + v.getDate() + "</date>\n");
		sb.append(indent + "\t<motif>"+ v.getMotif() + "</motif>\n");
		sb.append(indent + "\t<ct>"+ v.getCt() + "</ct>\n");
		sb.append(indent + "</voiture>");
		return sb.toString();
	}
	
	public String visiteMoto(Moto m, String indent) {
		StringBuilder sb = new StringBuilder();
		sb.append(indent + "<moto>\n");
		sb.append(indent + "\t<date>" + m.getDate() + "</date>\n");
		sb.append(indent + "\t<motif>"+ m.getMotif() + "</motif>\n");
		sb.append(indent + "\t<sd>"+ m.isSd() + "</sd>\n");
		sb.append(indent + "</moto>");
		return sb.toString();
	}
}
